package com.cloudkinto.common.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Random;

public class MathUtils {
    public static Integer parse(String source) {
        if (source.contains("万")) {
            source = source.replace("万", "").replace("+", "");
            Double d = Double.valueOf(source) * 10000;
            return d.intValue();
        } else if (source.contains("-")) {
            return null;
        } else {
            return Integer.valueOf(source);
        }
    }

    public static Double numberFormat(double totalCost) {
        if (Double.isInfinite(totalCost) || Double.isNaN(totalCost))
            return Double.NaN;
        return new BigDecimal(totalCost + "").setScale(2, RoundingMode.HALF_UP).doubleValue();
    }


    public static Double numberFormat(double totalCost, int scale) {
        if (Double.isInfinite(totalCost) || Double.isNaN(totalCost))
            return Double.NaN;
        return new BigDecimal(totalCost + "").setScale(scale, RoundingMode.HALF_UP).doubleValue();
    }

    public static Double numberFormat(double totalCost, int scale, RoundingMode mode) {
        if (Double.isInfinite(totalCost) || Double.isNaN(totalCost))
            return Double.NaN;
        return new BigDecimal(totalCost + "").setScale(scale, mode).doubleValue();
    }

    public static Float numberFormat(float totalCost, int scale) {
        if (Double.isInfinite(totalCost) || Double.isNaN(totalCost))
            return Float.NaN;
        return new BigDecimal(totalCost + "").setScale(scale, RoundingMode.HALF_UP).floatValue();
    }


    public static Double numberFormat(int scale, double totalCost) {
        if (Double.isInfinite(totalCost) || Double.isNaN(totalCost))
            return Double.NaN;
        return new BigDecimal(totalCost + "").setScale(scale, RoundingMode.HALF_UP).doubleValue();
    }


    public static Double numberFormat(String totalCost) {
        return new BigDecimal(totalCost).setScale(2, RoundingMode.HALF_UP).doubleValue();
    }

    public static String numberFormat(String totalCost, int scale) {
        return new BigDecimal(totalCost).setScale(scale, RoundingMode.HALF_UP).toString();
    }


    public static double add(double... v1) {
        BigDecimal b2 = new BigDecimal(Double.toString(0));

        for (int i = 0; i < v1.length; i++) {
            BigDecimal b1 = new BigDecimal(Double.toString(v1[i]));
            b2 = b1.add(b2);
        }
        return b2.doubleValue();
    }

    public static float add(float... v1) {
        BigDecimal b2 = new BigDecimal(Float.toString(0));

        for (int i = 0; i < v1.length; i++) {
            BigDecimal b1 = new BigDecimal(Float.toString(v1[i]));
            b2 = b1.add(b2);
        }
        return b2.floatValue();
    }


    public static double sub(double... v1) {
        BigDecimal b2 = new BigDecimal(Double.toString(v1[0]));

        for (int i = 1; i < v1.length; i++) {
            BigDecimal b1 = new BigDecimal(Double.toString(v1[i]));
            b2 = b2.subtract(b1);
        }
        return b2.doubleValue();
    }


    public static double div(double... v1) {
        BigDecimal b2 = new BigDecimal(Double.toString(v1[0]));

        for (int i = 1; i < v1.length; i++) {
            BigDecimal b1 = new BigDecimal(Double.toString(v1[i]));
            b2 = b2.divide(b1, 10, RoundingMode.HALF_UP);
        }
        return b2.doubleValue();
    }

    public static float div(float... v1) {
        BigDecimal b2 = new BigDecimal(Float.toString(v1[0]));

        for (int i = 1; i < v1.length; i++) {
            BigDecimal b1 = new BigDecimal(Float.toString(v1[i]));
            b2 = b2.divide(b1, 10, RoundingMode.HALF_UP);
        }
        return b2.floatValue();
    }


    public static BigDecimal divBig(int scale, double... v1) {
        BigDecimal b2 = new BigDecimal(Double.toString(v1[0]));

        for (int i = 1; i < v1.length; i++) {
            BigDecimal b1 = new BigDecimal(Double.toString(v1[i]));
            b2 = b2.divide(b1, scale, RoundingMode.HALF_UP);
        }
        return b2;
    }

    public static double div(int scale, double... v1) {
        return divBig(scale, v1).doubleValue();
    }

    public static double mul(double... v1) {
        BigDecimal b2 = new BigDecimal(Double.toString(v1[0]));

        for (int i = 1; i < v1.length; i++) {
            BigDecimal b1 = new BigDecimal(Double.toString(v1[i]));
            b2 = b2.multiply(b1);
        }
        return b2.doubleValue();
    }


    public static double mul(float... v1) {
        BigDecimal b2 = new BigDecimal(Float.toString(v1[0]));

        for (int i = 1; i < v1.length; i++) {
            BigDecimal b1 = new BigDecimal(Float.toString(v1[i]));
            b2 = b2.multiply(b1);
        }
        return b2.floatValue();
    }

    /**
     * 方法名:         calcFloatValue
     * 方法功能描述:   float类型的加减乘除，避免Java 直接计算错误
     *
     * @param: type:   加：add   减：  subtract  乘：multiply 除：divide
     */
    public static float calcFloatValue(float t1, float t2, String type) {
        BigDecimal a = new BigDecimal(String.valueOf(t1));
        BigDecimal b = new BigDecimal(String.valueOf(t2));
        float retValue = 0f;
        switch (type) {
            case "add":
                retValue = a.add(b).floatValue();
                break;
            case "subtract":
                retValue = a.subtract(b).floatValue();
                break;
            case "multiply":
                retValue = a.multiply(b).floatValue();
                break;
            case "divide":
                retValue = a.divide(b, 4, RoundingMode.HALF_UP).floatValue();
                break;
        }
        return retValue;
    }



    public static int random(int min, int max) {
        Random random = new Random();
        //随机数7-15
        return random.nextInt(max- min+1) + min;
    }
}
